<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getMineScoreSortServe } from "@/api/scoringStatistics"

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "500px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    if (!this.chart) {
      this.chart = echarts.init(this.$el, "macarons");
    }
    this.getMineScoreSort();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getMineScoreSort() {
      getMineScoreSortServe().then(res => {
        if (res.code == 200) {
          let yAxisData = [], seriesData = [], dataObj = {};
          res.data.forEach(el => {
            yAxisData.push(el.score);
            seriesData.push(el.number)
            dataObj[el.score] = el;
          });
          this.initChart(yAxisData, seriesData, dataObj);
        }
      })
    },
    initChart(yAxisData, seriesData, dataObj) {
      this.chart.setOption({
        title: {
          text: "计分场景排名",
          textStyle: {
            //标题内容的样式
            color: "#1e2c3c",
            fontStyle: "normal", //主标题文字字体风格，默认normal，有italic(斜体),oblique(斜体)
            fontWeight: "800", //可选normal(正常)，bold(加粗)，bolder(加粗)，lighter(变细)，100|200|300|400|500...
            fontFamily: "san-serif", //主题文字字体，默认微软雅黑
            fontSize: 16, //主题文字字体大小，默认为18px

          },
        },
        tooltip: {
          trigger: "axis",
          backgroundColor:"#fff",
          formatter(params, ticket, callback) {
            let res = `
                  <div style="width: 300px; white-space: pre-wrap;display: grid;color:#000">
                      <p style="margin: 0">${params[0].name}：${dataObj[params[0].name].mineScoreText}</p>
                      <p>总数：${dataObj[params[0].name].number}</p>
                  </div>
                `
            return res
          }
        },
        legend: {
          show: false, // 是否显示图例
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
          boundaryGap: [0, 0.01],
        },
        yAxis: {
          type: "category",
          data: yAxisData,
        },
        series: [
          {
            // name: "2011",
            type: "bar",
            data: seriesData,
            label: {
              show: true,
              position: "right", // 顶部显示
              // formatter: "{c}",
            },
          },
        ],
      });
    },
  },
};
</script>
